package com.nnfe.fyd.feature.inquiry;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.nnfe.fyd.BusKey;
import com.nnfe.fyd.R;
import com.nnfe.fyd.ShareKey;
import com.nnfe.fyd.base.ActionBarFragment;
import com.nnfe.fyd.base.NBaseAdapter;
import com.nnfe.fyd.bean.HttpResult;
import com.nnfe.fyd.bean.myinquirt.InquirtBean;
import com.nnfe.fyd.feature.inquiryPrice.InquiryPriceActivity;
import com.nnfe.fyd.feature.inquiryreport.InquiryReportAcivity;
import com.nnfe.fyd.feature.login.LoginActivity;
import com.nnfe.fyd.http.CallbackImpl;
import com.nnfe.fyd.utils.SharePrefUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by dada on 2017/3/21.
 */

public class MyInquiryFragment extends ActionBarFragment {

    @Bind(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    @Bind(R.id.list)
    ListView list;

    MyInquiryAdaper myInquiryAdaper;

    @Override
    public Object getTitleString() {
        return "订单";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_list;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        donet();

        mActionBar.getRightButton().setText("新建");
        mActionBar.getRightButton().setVisibility(View.VISIBLE);
    }

    @Override
    public void onRightButtonClick() {
        super.onRightButtonClick();
        if (!SharePrefUtil.getBoolean(mContext, ShareKey.isLogin, false)) {
            LoginActivity.lauch(mContext, LoginActivity.TYPE_LOGIN);
        } else {
            startActivity(new Intent(mContext, InquiryPriceActivity.class));
        }
    }

    private void donet() {
        String id = SharePrefUtil.getString(mContext, ShareKey.UserId);
        service.inquiryList("0", "100", id).enqueue(new CallbackImpl<InquirtBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<InquirtBean>> call, Response<HttpResult<InquirtBean>> response) {
                if (myInquiryAdaper == null) {
                    myInquiryAdaper = new MyInquiryAdaper(mContext);
                }
                myInquiryAdaper.clear();
                myInquiryAdaper.setData(response.body().data.content);
                list.setAdapter(myInquiryAdaper);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String str) {
        if (str.equalsIgnoreCase(BusKey.POST_inquiryReport)) {
            donet();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        EventBus.getDefault().register(this);
        initView();
        return rootView;
    }

    @Override
    protected void initView() {

        swipeRefresh.setEnabled(false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }


    class MyInquiryAdaper extends NBaseAdapter<InquirtBean.Inquirt> {


        public MyInquiryAdaper(Context context) {
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {

                convertView = View.inflate(mContext, R.layout.layout_inquiry, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }


            final InquirtBean.Inquirt bean = getItem(position);
            viewHolder.danhao.setText("单号：" + bean.id);
            if (TextUtils.isEmpty(bean.roomAddress) || TextUtils.isEmpty(bean.housingArea)) {
                viewHolder.zhuzhi.setText("有房本");
                viewHolder.mianji.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.zhuzhi.setText("房屋地址：" + bean.roomAddress);
                viewHolder.mianji.setText("建筑面积：" + bean.housingArea + "平米");
                viewHolder.zhuzhi.setVisibility(View.VISIBLE);
                viewHolder.mianji.setVisibility(View.VISIBLE);
            }


            if (TextUtils.isEmpty(bean.loanLimit) || bean.loanLimit.equalsIgnoreCase("0.0")) {
                viewHolder.baodan.setBackgroundColor(mContext.getResources().getColor(R.color.gray));
                viewHolder.baodan.setOnClickListener(null);
                viewHolder.baodan.setText("报单");
            } else {
                if (TextUtils.isEmpty(bean.loanAmount)) {
                    viewHolder.baodan.setBackgroundColor(mContext.getResources().getColor(R.color.btn_background_nomal));
                    viewHolder.baodan.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            InquiryReportAcivity.lauch(mContext, bean.id, bean.loanLimit);
                        }
                    });
                    viewHolder.baodan.setText("报单");
                } else {
                    viewHolder.baodan.setBackgroundColor(mContext.getResources().getColor(R.color.gray));
                    viewHolder.baodan.setText("已报单");
                    viewHolder.baodan.setOnClickListener(null);

                }
            }


            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MyinquiryDeailActivity.lauch(mContext, bean);
                }
            });

            return convertView;
        }


        class ViewHolder {
            @Bind(R.id.danhao)
            TextView danhao;
            @Bind(R.id.zhuzhi)
            TextView zhuzhi;
            @Bind(R.id.mianji)
            TextView mianji;
            @Bind(R.id.details)
            TextView details;
            @Bind(R.id.baodan)
            TextView baodan;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }


}
